<template>
  <div id="app">
    <router-view
      @change-theme="changeTheme"
      @change-manhuua="changeManhuua"
      :songId="songId"
      @data-all="dataAll"
      :dataAlls="dataAlls"
      :topTinement="topTinement"
      :classPageSixComics="classPageSixComics"
      @getSearchPageNum="getSearchPageNum"
      @search-sjuset="searchSjuset"
      :searchSesult="searchSesult"
      :historyCartoon="historyCartoon"
      :useraccountnumbers="useraccountnumbers"
      @user-accountnumber="accountnumber"
      @remove-his="removeHis"
      :fig="fig"
      @change-bangdan="changeBangdan"
      :channgeBangdans="channgeBangdans"
      :collectDataalls="collectDataalls"
      :bangdanId="bangdanId"
      @remove-historys-cartoon="removeHistorysCartoon"
      @change-todo="changeTodo"
      @get-useraccountnumbers="getUseraccountnumbers"
      :homeMsg="homeMsg"
    ></router-view>
  </div>
</template>



<script>
import { setTheme } from "@/assets/theme.js";
import {
  getPaihangbang,
  getSearchReemont,
  getHomeViewAllData,
  getBangdanId,
  getLayoutId,
} from "@/api/index.js";
export default {
  components: {},
  data() {
    return {
      songId: null,
      dataAlls: [],
      pageNum: 1,
      topTinement: null,
      classPageSixComics: [],
      searchPageNum: 1,
      searchSesult: null,
      historyCartoon:
        JSON.parse(window.localStorage.getItem("hismanhua")) == null
          ? []
          : JSON.parse(window.localStorage.getItem("hismanhua")),
      useraccountnumbers:
        JSON.parse(window.localStorage.getItem("useraccountnumber")) == null
          ? []
          : JSON.parse(window.localStorage.getItem("useraccountnumber")),
      fig: 0,
      collectDataalls: [],
      channgeBangdans: {},
      bangdanId: null,
      homeMsg:'',
    };
  },

  methods: {
    getUseraccountnumbers(){
      this.useraccountnumbers = JSON.parse(window.localStorage.getItem("useraccountnumber"))
    },
    changeTodo() {
      setTheme("dark");
    },
    removeHistorysCartoon(id) {
      this.historyCartoon = this.historyCartoon.filter((el) => {
        return el.id != id;
      });
      window.localStorage.setItem(
        "hismanhua",
        JSON.stringify(this.historyCartoon)
      );
    },
    changeBangdan(obj) {
      this.channgeBangdans = obj;
    },
    getLayoutData() {
      this.bangdanId.forEach((element) => {
        getLayoutId(element.id)
          .then((result) => {
            let obj = result.data.data.list;
            this.collectDataalls.push(obj);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },

    removeHis() {
      this.useraccountnumbers = null;
    },
    accountnumber(data) {
      this.useraccountnumbers = data;
    },
    searchSjuset(data) {
      this.$emit("search-sjuset", data);
      this.searchSesult = data;
    },
    GetClassPageSixComics() {
      //搜索页面推荐
      getSearchReemont(this.searchPageNum)
        .then((res) => {
          let data = res.data.data.roll_six_comics;
          data.forEach((element) => {
            this.classPageSixComics.push(element);
            this.homeMsg=""
            this.fig = 0;
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getSearchPageNum() {
      this.homeMsg="页面加载中。。。"
      this.fig++;
      if (this.fig == 1) {
        this.searchPageNum++;
        this.GetClassPageSixComics();
      }
    },
    getDataAll() {
      this.homeMsg='页面加载中。。。'
      //得到主页面全部漫画
      getHomeViewAllData(this.pageNum)
        .then((res) => {
          let data = res.data.data;
          data.forEach((element) => {
            this.dataAlls.push(element);
          });
          this.homeMsg=''
          this.fig = 0;
        })
        .catch((error) => {
          this.fig = 0;
          console.log(error);
        });
    },
    dataAll() {
      this.fig++;
      if (this.fig == 1) {
        this.pageNum += 1;
        console.log(this.pageNum);
        this.getDataAll();
      }
    },
    changeManhuua(id) {
      if (this.songId == id) {
        return;
      }
      this.songId = id;
      this.$router.push(`/manhua?id=${this.songId}`);
      let ss = 0;

      this.historyCartoon.forEach((item) => {
        if (item.id == id) {
          ss = 1;
        }
      });
      if (ss > 0) {
        return;
      }
      this.historyCartoon.push({
        id,
        date: new Date().getTime(),
      });

      let Arrays = []; //存放去重过后的数据
      let obj = {};
      for (var i = 0; i < this.historyCartoon.length; i++) {
        if (!obj[this.historyCartoon[i].id]) {
          Arrays.push(this.historyCartoon[i]);
          obj[this.historyCartoon[i].id] = true;
        }
      }
      //attr：根据该属性排序；rev：升序1或降序-1，不填则默认为1
      function sortBy(attr, rev) {
        if (rev == undefined) {
          rev = 1;
        } else {
          rev ? 1 : -1;
        }
        return function (a, b) {
          a = a[attr];
          b = b[attr];
          if (a < b) {
            return rev * -1;
          }
          if (a > b) {
            return rev * 1;
          }
          return 0;
        };
      }

      Arrays.sort(sortBy("date", -1));
      window.localStorage.setItem("hismanhua", JSON.stringify(Arrays));
    },
    changeRouter() {
      setTimeout(() => {
        let ss = this.$route.path;
        if (ss == "/") {
          this.nums = 1;
        } else if (ss == "/search") {
          this.nums = 2;
        } else if (ss == "/collect") {
          this.nums = 3;
        } else if ((ss = "/user")) {
          this.nums = 4;
        }
      }, 500);
    },
    init() {
      setTheme("default"); // 初始化未默认主题
    },
    changeTheme(value) {
      setTheme(value);
    },
  },

  created() {
    this.init(); //初始化未默认主题
    this.changeRouter();
    this.getDataAll();
    this.GetClassPageSixComics();
    //得到排行榜前十
    getPaihangbang()
      .then((res) => {
        this.topTinement = res.data.data.list;
      })
      .catch((error) => {
        console.log(error);
      });

    let useraccountnumber =
      JSON.parse(window.localStorage.getItem("useraccountnumber")) == null
        ? {}
        : JSON.parse(window.localStorage.getItem("useraccountnumber"));

    this.useraccountnumbers = useraccountnumber;
    getBangdanId().then((res) => {
      this.bangdanId = res.data.data.list;
      this.getLayoutData();
    });
    this.$axios.get(`https://apis.netstart.cn/yunyuedu/book/getsub.json?id=13c58cc086f74e36978b4a7881b82517_4&title=%E5%A5%B3%E6%8E%8C%E4%BA%8B`).then((result) => {
      let ss = result.data.feed.entry
      let ssss ={}
        for (const key in ss) {
           let s =   key.split(':')[1]==undefined?key.split(':')[0] :key.split(':')[1] 
            ssss[s]=ss[key]
        }
        console.log(ssss);
    }).catch((err) => {
      console.log(err);
    });
  },
};
</script>
<style lang="less" scoped>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  background-color: @color2;
}
</style>
